package ldz.User;

import ldz.lian.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public int insertUser(String uno, String uname, String usex, String upassword, String utel, String uemail) {
        int result = 0;
        String sql = "INSERT INTO user (Uno, Uname, Usex, Upassword, Utel, Uemail) VALUES (?,?,?,?,?,?)";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, uno);
            preparedStatement.setString(2, uname);
            preparedStatement.setString(3, usex);
            preparedStatement.setString(4, upassword);
            preparedStatement.setString(5, utel);
            preparedStatement.setString(6, uemail);
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int deleteUser(String uno) {
        int result = 0;
        String sql = "DELETE FROM user WHERE Uno =?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, uno);
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<User> getAllUsers() {
        List<User> userList = new ArrayList<>();
        String sql = "SELECT Uno, Uname, Usex, Upassword, Utel, Uemail FROM user";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                User user = new User();
                user.setUno(resultSet.getString("Uno"));
                user.setUname(resultSet.getString("Uname"));
                user.setUsex(resultSet.getString("Usex"));
                user.setUpassword(resultSet.getString("Upassword"));
                user.setUtel(resultSet.getString("Utel"));
                user.setUemail(resultSet.getString("Uemail"));
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public int updateUser(String uno, String newPassword, String newEmail) {
        int result = 0;
        String sql = "UPDATE user SET Upassword =?, Uemail =? WHERE Uno =?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, newPassword);
            preparedStatement.setString(2, newEmail);
            preparedStatement.setString(3, uno);
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }


    @Override
    public List<User> queryUserByUno(String uno) {
        List<User> userList = new ArrayList<>();
        String sql = "SELECT * FROM user WHERE Uno =?";
        try (Connection connection = DBUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, uno);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                User user = new User();
                user.setUno(resultSet.getString("Uno"));
                user.setUname(resultSet.getString("Uname"));
                user.setUsex(resultSet.getString("Usex"));
                user.setUpassword(resultSet.getString("Upassword"));
                user.setUtel(resultSet.getString("Utel"));
                user.setUemail(resultSet.getString("Uemail"));
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }
}